package com.glowdayz.glowmee.bc;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.glowdayz.common.DBHandler;
import com.glowdayz.glowmee.dao.BrandDAO;
import com.glowdayz.glowmee.dao.FirstCategoryDAO;

public class InsertProductBC extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
       
    public InsertProductBC() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		
		int idBrand = 0;
		try {
			String sql = "SELECT idBrand FROM product ORDER BY create_date DESC LIMIT 0, 1";
			PreparedStatement pstmt = DBHandler.getInstance().getPrepareStatement(sql);
			try {
				ResultSet rs = pstmt.executeQuery();
				if (rs != null) {
					if (rs.next()) {
						idBrand = rs.getInt(1);
					}
					rs.close();
					rs = null;
				}
				pstmt.close();
				pstmt = null;
			} catch (SQLException e) {}
			
			request.setAttribute("idBrand", idBrand);
			request.setAttribute("brand", BrandDAO.getInstance().getBrandList(1, -1, "brandTitle",  "b.isDisplay=1"));
			request.setAttribute("first", FirstCategoryDAO.getInstance().getFirstCategoryList(1, -1, "isDisplay=1"));
		} catch (Exception e) {
			DBHandler.getInstance().disconnect();
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/product/insertProduct.jsp");
		dpr.forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

}